package com.loiot.baqi.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.loiot.baqi.constant.Const;
import com.loiot.baqi.constant.URLConst;
import com.loiot.baqi.controller.response.AjaxResponse;
import com.loiot.baqi.controller.response.Pager;
import com.loiot.baqi.pojo.File;
import com.loiot.baqi.pojo.TgQqGroup;
import com.loiot.baqi.pojo.WeiAccountQqGroup;
import com.loiot.baqi.pojo.WeiAccountQqGroupBusiness;
import com.loiot.baqi.service.AccountService;
import com.loiot.baqi.service.FileService;
import com.loiot.baqi.service.TgQqGroupService;
import com.loiot.baqi.service.WeiAccountQqGroupBusinessService;
import com.loiot.baqi.service.WeiAccountQqGroupService;
import com.loiot.baqi.status.AssignmentStatus;
import com.loiot.baqi.status.PauseStartType;
import com.loiot.baqi.status.PlatAccountType;
import com.loiot.baqi.status.QqGroupCooperationStatus;
import com.loiot.commons.message.util.JsonUtil;
import com.timeloit.pojo.Account;


/**
 * 商务谈判 处理器。
 * @author  wangzx 
 * @creation 2016-08-11
 */


@Controller
@RequestMapping(value = "/tgQqGroupCooperation")
public class TgQqGroupCooperationController {
    
    public static final AjaxResponse NUMBER_EXIST= new AjaxResponse(-100, "群号已存在");
    
    private Logger log = LoggerFactory.getLogger(this.getClass());
    
    @Resource
	private TgQqGroupService tgQqGroupService;
    @Resource
	private FileService fileService;
	
    @Resource
	private WeiAccountQqGroupBusinessService weiAccountQqGroupBusinessService;
    @Resource
    private WeiAccountQqGroupService weiAccountQqGroupService;
    
    /**
     * 后台用户业务逻辑
     */
    @Resource
    private AccountService accountService;
    
	private HashMap<String,Object> pmap = new HashMap<String,Object>();
	
	/**
     * 跳转 QQ群组列表页.
     * 
     * @return 模板位置
     */
    @RequestMapping(value = "/list")
    public String list(@RequestParam(value = "pi", defaultValue = "0") int pageIndex,
    		@RequestParam(value = "jsonParam", defaultValue = "{}") String jsonParam,
    	TgQqGroup p, ModelMap model)throws Exception {
    	HashMap<String,Object> paramMap=this.getParaMap(jsonParam, model);
    	paramMap.put("qtype", "like");
    	//用户数据过滤
    	/*
    	if(UserSessionUtils.getAccountType()==AccountType.HR.getCode() || UserSessionUtils.getAccountType()==AccountType.JOB_HUNTER.getCode() ){
    		paramMap.put("inPerson", UserSessionUtils.getAccount().getAccountId());
    	}*/
    	System.out.print("");
        Pager<TgQqGroup> pager = tgQqGroupService.queryTgQqGroupListPage(paramMap , pageIndex);
        model.put("pager", pager);
        model.put("jsonParam", jsonParam);
        return "/tgQqgroupCooperation/tgQqgroupCooperation_list";
    }
    
    /**
     * 获取查询条件.
     * @param jsonParam
     * @param model
     * @return
     */
    public HashMap<String,Object> getParaMap(String jsonParam,ModelMap model){
    	HashMap<String,Object> newParamMap = newParamMap =  new HashMap<String,Object>();
    	 HashMap<String,Object> paramMap =JsonUtil.toObject(jsonParam, HashMap.class);
		Iterator iter = paramMap.entrySet().iterator();
		while (iter.hasNext()) {
		Map.Entry entry = (Map.Entry) iter.next();
    		Object key = entry.getKey();
    		Object val = entry.getValue();
    		if(key.toString().equals("name")){
    			newParamMap.put("nameT", val);
    		}else
    		if(String.valueOf(val).length()>0){
    			newParamMap.put(String.valueOf(key), val);
        		model.put(String.valueOf(key), val);
    		}
		}
		return newParamMap;
    }

    
    /**
     * 更新状态
     * 
     * @param id TgQqGroupID
     */
    @RequestMapping(value = "/modifyStatus")
    public String modifyStatus(@RequestParam(value = "id", required = true) java.lang.Long id,@RequestParam(value = "leaderCooperationStatus", required = true) Integer leaderCooperationStatus,HttpServletRequest request)throws Exception {
    	TgQqGroup p = new TgQqGroup();
    	//p.setLeaderCooperationStatus(leaderCooperationStatus);
    	p.setId(id);
    	 //已合作    
        if(leaderCooperationStatus.intValue()==QqGroupCooperationStatus.WAIT_COOPERATION.getCode())
        {
        	/*p.setLeaderCooperationEndFeedback("");
        	p.setLeaderCooperationFeedback("");*/
        }
    	tgQqGroupService.updateTgQqGroup(p);
    	String s = request.getHeader("Referer");
        String redirectStr = s.substring(s.indexOf("/tgQqGroupCooperation/"), s.length());
        return "redirect:"+redirectStr;
    }
    /**
     * 跳转到拒绝页面
     * 
     * @param id TgQqGroupID
     */
    @RequestMapping(value = "/toRejectPage")
    public String toRejectPage(HttpServletRequest request)throws Exception {
        return "/tgQqGroupCooperation/reject_cooperation_modal";
    }
    
    /**
     * 拒绝合作
     * 
     * @param id TgQqGroupID
     */
    @RequestMapping(value = "/addRejectCooperation")
    @ResponseBody
    public Object addRejectCooperation(HttpServletRequest request)throws Exception {
        String groupId = request.getParameter("groupId");
        String sug_content = request.getParameter("sug_content");
        TgQqGroup group = this.tgQqGroupService.getTgQqGroupById(Long.parseLong(groupId));
        /*//还未添加合作    
        if(group.getLeaderCooperationStatus()==QqGroupCooperationStatus.WAIT_COOPERATION.getCode())
        {
        	group.setLeaderCooperationFeedback(sug_content);
        }
        //还已合作    
        if(group.getLeaderCooperationStatus()==QqGroupCooperationStatus.WAIT_COOPERATION.getCode())
        {
        	group.setLeaderCooperationEndFeedback(sug_content);
        }
        group.setLeaderCooperationStatus((int)QqGroupCooperationStatus.REJECT_COOPERATION.getCode());
        this.tgQqGroupService.updateTgQqGroup(group);*/
        return AjaxResponse.OK;
    }
    
    /**
     * 查询未分配qq群的
     * 
     * @return 模板位置
     */
    @RequestMapping(value = "/noAssignmentList")
    public String queryNoAssignmentList(@RequestParam(value = "pi", defaultValue = "0") int pageIndex,
    		@RequestParam(value = "jsonParam", defaultValue = "{}") String jsonParam,
    	TgQqGroup p, ModelMap model)throws Exception {
    	HashMap<String,Object> paramMap=this.getParaMap(jsonParam, model);
    	//paramMap.put("qtype", "like");
    	paramMap.put("businessAssignmentStatus",AssignmentStatus.UNASSIGNED.getCode() );
    	paramMap.put("orderColumn", "qg.id desc");

    	//用户数据过滤
    	/*
    	if(UserSessionUtils.getAccountType()==AccountType.HR.getCode() || UserSessionUtils.getAccountType()==AccountType.JOB_HUNTER.getCode() ){
    		paramMap.put("inPerson", UserSessionUtils.getAccount().getAccountId());
    	}*/
    	System.out.print("");
        Pager<TgQqGroup> pager = tgQqGroupService.queryTgQqGroupListPage(paramMap , pageIndex);
        model.put("pager", pager);
        model.put("jsonParam", jsonParam);
        return "/tgQqGroupCooperation/no_assignment_group_list";
    }
    
    /**
	 * 跳转到分配用户页面.
	 * 
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/toAssign")
	public String list(
			@RequestParam(value = "pi", defaultValue = "0") int pageIndex,
			@RequestParam(value = "jsonParam", defaultValue = "{}") String jsonParam,
			Account p, ModelMap model,
			@RequestParam(value = "groupIds", required = true) String groupIds) throws Exception {
		HashMap<String, Object> paramMap = this.getParaMap(jsonParam, model);
		
		paramMap.put("qtype", "like");
		paramMap.put("type", PlatAccountType.WEI_ACCOUNT_BUSINESS.getCode());
		paramMap.put("isDelete", PauseStartType.START.getCode());
		paramMap.put("groupIds", groupIds);
		
		Pager<Account> pager = accountService.getFlatAccountListPage(paramMap, pageIndex);
		model.put("pager", pager);
		model.put("jsonParam", jsonParam);
		model.put("groupIds", groupIds);
		model.put("menuClass", "accountManage");
		return "/tgQqGroupCooperation/weiAccountQqGroup_assign";
	}
	//跳转到群详情页面，包括群基本信息、微用户信息，微用户历史信息，商务谈判人信息，商务谈判人历史信息
    @RequestMapping(value = "/toView")
    public String toViewWeiAccountQqGroupBusiness(@RequestParam(value = "id", required = true) java.lang.Long id, ModelMap model,HttpSession session)throws Exception {
    	if(id==null){
    		return URLConst.ERROR_URL;
    	}
    	Account account = (Account) session
				.getAttribute(Const.SESSION_USER_KEY);
		Map<String,Object> p = weiAccountQqGroupBusinessService.getQqGroupAllInfoByGroupId(id,account.getType());
		if (p == null) {
//			return AjaxResponse.NOEXITS;
		}
		model.put("busiList", p.get("busiList"));
		model.put("weiList", p.get("weiList"));
		model.put("qqGroup", p.get("qqGroup"));
		model.put("currBusinessPerson", p.get("currBusinessPerson"));
		model.put("currWeiAccount", p.get("currWeiAccount"));
    	//model.put("p", weiAccountQqGroupBusinessService.getWeiAccountQqGroupBusinessById(id));
    	 model.put("pid",  id);
    	return "/tgQqGroupCooperation/qqGroupCooperationAllInfo_view";
    }
    //跳转到群分配的微用户详细信息，或群分配的商务谈判人详细信息
    @RequestMapping(value = "/toDividerDetail")
    public String toDividerDetail(@RequestParam(value = "id", required = true) java.lang.Long id,String type, ModelMap model,HttpSession session)throws Exception {
    	if(id==null || type==null){
    		return URLConst.ERROR_URL;
    	}

    	if(type.equals("8")){
        	WeiAccountQqGroup weiAccountVo = weiAccountQqGroupService.getWeiAccountQqGroupById(id);
        	model.put("currWeiAccount", weiAccountVo);
    	}else if(type.equals("9")){
    		HashMap<String,Object> pMap = new HashMap<String,Object>();
    		pMap.put("id", id);
        	WeiAccountQqGroupBusiness BusinessVo = weiAccountQqGroupBusinessService.getWeiAccountQqGroupBusiness(pMap);
        	model.put("currBusinessPerson", BusinessVo);
    	}else{
    		return URLConst.ERROR_URL;
    	}
    	model.put("type", type);
//    	 model.put("pid",  id);
    	return "/tgQqGroupCooperation/weiAccountOrBusinesserDetail_view";
    }
    

	/**
	 * ajax 根据id查询实体bean
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getQqGroupAllInfoByGroupId")
//	@ResponseBody
	public Object ajaxGetById(
			@RequestParam(value = "id", required = true) java.lang.Long id,HttpSession session,ModelMap model)
			throws Exception {
		Account account = (Account) session
				.getAttribute(Const.SESSION_USER_KEY);
		Map<String,Object> p = weiAccountQqGroupBusinessService.getQqGroupAllInfoByGroupId(id,account.getType());
		if (p == null) {
			return AjaxResponse.NOEXITS;
		}
		model.put("busiList", p.get("busiList"));
		model.put("weiList", p.get("weiList"));
		model.put("qqGroup", p.get("qqGroup"));
		return AjaxResponse.OK(p);
	}
	
	  /**
     * ajax 根据id查询实体bean
     * @return
     */
    @RequestMapping(value = "/getVoucherImagByDataId")
    @ResponseBody
    public Object getVoucherImagByDataId(@RequestParam(value = "id", required = true) java.lang.Long id)throws Exception {
     	HashMap<String,Object> pMap  =  new HashMap<String,Object>();
    	//用户数据过滤
//     	if(UserSessionUtils.getAccountType()==AccountType.HR.getCode() || UserSessionUtils.getAccountType()==AccountType.JOB_HUNTER.getCode() ||  UserSessionUtils.getAccountType()==AccountType.TECHICAL_AUDIT.getCode()){
//    		  p = zpJlInfoService.getZpJlInfoById(id, UserSessionUtils.getAccount().getAccountId());
//    	} else {
//  		      p = zpJlInfoService.getZpJlInfoById(id);
//    	}
     	pMap.put("dataId", id);
    	List<File> fileList = fileService.queryFileList(pMap);
    	if(fileList==null){
    		return AjaxResponse.NOEXITS;
    	}
    	
    	return AjaxResponse.OK(fileList);
    }

    
}
